<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入登录名"
        v-model="queryForm.name"
      >
      </el-input>
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入手机号"
        v-model="queryForm.phone"
      >
      </el-input>
      <el-select
        clearable
        style="width: 150px"
        class="filter-item"
        v-model="queryForm.status"
        placeholder="请选择用户状态"
      >
        <el-option
          v-for="(val, key) in Dicts.CONSUMER_STATUS"
          :key="key"
          :label="val"
          :value="key"
        >
        </el-option>
      </el-select>
      <el-button
        class="filter-item"
        @click="doQuery"
        type="primary"
        icon="el-icon-search"
        >查询</el-button
      >
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        @click="openCreateDialog"
        type="success"
        icon="el-icon-edit"
        >新增</el-button
      >
    </div>
    <el-table
      v-loading="loading"
      @sort-change="handleSortChange"
      :data="records"
      style="width: 100%"
    >
      <el-table-column prop="rowId" label="ID" width="100" align="center">
      </el-table-column>
      <el-table-column prop="name" label="登录名" width="150" align="center">
      </el-table-column>
      <el-table-column prop="phone" label="电话号码" width="150" align="center">
      </el-table-column>
      <el-table-column prop="realName" label="真名" width="150" align="center">
      </el-table-column>
      <el-table-column
        prop="nickName"
        label="物业人员昵称"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="headPhoto"
        label="物业成员头像"
        width="150"
        align="center"
      >
        <template slot-scope="scope" v-if="scope.row.headPhoto != null">
          <div v-viewer>
            <img
              :src="$oss(scope.row.headPhoto)"
              alt
              style="width:30px;height:30px;border-radius:5px;"
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="isPush"
        label="是否推送"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.CONSUMER_STATUS[scope.row.isPush] }}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="150" align="center">
        <template slot-scope="scope">
          {{ Dicts.CONSUMER_STATUS[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        sortable="custom"
        sort-by="create_time"
        label="创建时间"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        width="300"
        fixed="right"
        align="center"
        label="操作"
        class-name="small-padding"
      >
        <template slot-scope="scope">
          <el-button
            @click="queryCommunity(scope.row.userId)"
            type="default"
            size="mini"
            icon="el-icon-search"
            title="查看小区"
            circle
          ></el-button>
          <el-button
            @click="editPropertyUser(scope.row)"
            type="primary"
            size="mini"
            icon="el-icon-edit"
            title="编辑"
            circle
          ></el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.rowId)"
            icon="el-icon-delete"
            circle
            title="删除"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>

    <el-dialog
      :title="`新增物业人员`"
      :visible.sync="dialogFormVisible"
      width="40%"
    >
      <el-form
        :model="addForm"
        label-width="90px"
        :rules="addFormRules"
        ref="addForm"
      >
        <el-row>
          <el-col :span="10">
            <el-form-item label="登录名" prop="name">
              <el-input
                v-model="addForm.name"
                auto-complete="off"
                placeholder="请输入4-20位数字或字母"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="真实姓名" prop="realName">
              <el-input
                v-model="addForm.realName"
                auto-complete="off"
                maxlength="15"
                placeholder="请输入中文"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="密码" prop="passwordCheck">
              <el-input
                type="password"
                v-model="addForm.passwordCheck"
                auto-complete="off"
                placeholder="6-20位且包含大小写英文字母和数字"
                maxlength="20"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="确认密码" prop="confirmPassword">
              <el-input
                type="password"
                v-model="addForm.confirmPassword"
                auto-complete="off"
                placeholder="6-20位且包含大小写英文字母和数字"
                maxlength="20"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="手机" prop="phone">
              <el-input
                v-model="addForm.phone"
                auto-complete="off"
                placeholder="请输入11位手机号"
                maxlength="11"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="是否推送">
              <el-switch
                v-model="value3"
                active-text="是"
                inactive-text="否"
              ></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveExampleForm">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :title="`编辑物业人员`"
      :visible.sync="dialogEditFormVisible"
      width="20%"
    >
      <el-form
        :model="editExample"
        label-width="90px"
        :rules="editExampleRules"
        ref="editExample"
      >
        <el-form-item label="真实姓名" prop="realName">
          <el-input
            v-model="editExample.realName"
            auto-complete="off"
            maxlength="15"
            placeholder="请输入中文"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input
            v-model="editExample.phone"
            auto-complete="off"
            placeholder="请输入11位手机号"
            maxlength="11"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否推送">
          <el-switch
            v-model="value2"
            active-text="是"
            inactive-text="否"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editExampleForm">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :title="`查看物业人员下的小区`"
      :visible.sync="detailFormVisible"
      width="50%"
    >
      <el-table v-loading="loading" :data="detailRecords" style="width: 100%">
        <el-table-column prop="rowId" label="编号" width="100" align="center">
        </el-table-column>
        <el-table-column
          prop="phone"
          label="电话号码"
          width="150"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="realName"
          label="真名"
          width="150"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="communityName"
          label="小区名"
          width="250"
          align="center"
        >
        </el-table-column>
        <el-table-column
          width="200"
          align="center"
          label="操作"
          class-name="small-padding"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click="deleteCommunity(scope.row)"
              icon="el-icon-delete"
              circle
              title="删除"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryPropertyUser,
  deletePropertyUser,
  addPropertyUser,
  editPropertyUser,
  selectPropertyUserCommunity,
  deletePropertyUserCommunity
} from '@/api/property'
import 'viewerjs/dist/viewer.css'
import Viewer from 'v-viewer'
import Vue from 'vue'
Vue.use(Viewer)
import md5 from 'js-md5'

export default {
  data() {
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入登录名'))
      }
      if (!/^[0-9a-zA-Z]{4,20}$/.test(value)) {
        callback(new Error('请输入4-20位数字或字母'))
      }
      callback()
    }
    var checkRealName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入真实姓名'))
      }
      if (value.indexOf(' ') >= 0) {
        callback(new Error('请勿输入带有空格的姓名'))
      }
      if (!/^[\u4e00-\u9fa5]+$/.test(value)) {
        callback(new Error('请输入正确的姓名'))
      }
      callback()
    }
    var checkPassword = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入密码'))
      }
      if (!/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(.{6,20})$/.test(value)) {
        callback(new Error('请输入6-20位且包含大小写英文字母和数字的密码!'))
      } else {
        callback()
      }
    }
    var checkConfirmPassword = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入确认密码'))
      }
      if (value !== this.addForm.passwordCheck) {
        callback(new Error('两次密码不一致'))
      } else {
        callback()
      }
    }
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入手机号'))
      }
      if (!/^(1[345789]\d{9})$/.test(value)) {
        callback(new Error('请输入正确的号码'))
      } else {
        callback()
      }
    }
    return {
      detailFormVisible: false,
      value3: false,
      value2: false,
      dialogEditFormVisible: false,
      dialogFormVisible: false,
      loading: false,
      addForm: {
        rowId: undefined,
        type: '',
        title: '',
        status: ''
      },
      addFormRules: {
        name: [{ required: true, validator: checkName, trigger: 'blur' }],
        realName: [
          { required: true, validator: checkRealName, trigger: 'blur' }
        ],
        passwordCheck: [
          { required: true, validator: checkPassword, trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, validator: checkConfirmPassword, trigger: 'blur' }
        ],
        phone: [{ required: true, validator: checkPhone, trigger: 'blur' }]
      },
      exampleForm: [],
      editExample: [],
      editExampleRules: {
        phone: [{ required: true, validator: checkPhone, trigger: 'blur' }]
      },
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        ascs: '',
        descs: '',
        importance: undefined,
        title: undefined
      },
      typeOptions: Dicts.EXAMPLE_TYPE,
      records: [],
      passwordCheck: ''
    }
  },
  created() {
    this.doQuery()
  },
  methods: {
    handleSortChange({ column, prop, order }) {
      this.queryForm.ascs = ''
      this.queryForm.descs = ''
      if (order) {
        if (order === 'ascending') {
          this.queryForm.ascs = column.sortBy
        } else {
          this.queryForm.descs = column.sortBy
        }
      }
      this.queryForm.current = 0
      this.doQuery()
    },
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      queryPropertyUser(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
        this.loading = false
      })
    },
    queryCommunity(userId) {
      selectPropertyUserCommunity(userId).then(({ data: { data } }) => {
        this.detailFormVisible = true
        this.detailRecords = data
        this.loading = false
      })
    },
    deleteCommunity({ rowId, userId }) {
      this.$confirm('确定要解除物业人员与该小区的关系?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePropertyUserCommunity(rowId).then(({ data: { data, msg } }) => {
          this.queryCommunity(userId)
          this.$message({
            showClose: true,
            message: msg,
            type: 'success'
          })
        })
      })
    },
    handleDelete(rowId) {
      this.$confirm('此操作将永久删除该人员, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePropertyUser(rowId).then(({ data: { data, msg } }) => {
          this.doQuery()
          this.$message({
            showClose: true,
            message: msg,
            type: 'success'
          })
        })
      })
    },
    openCreateDialog() {
      this.addForm = {}
      this.dialogFormVisible = true
      this.$refs.addForm && this.$refs.addForm.resetFields()
    },
    saveExampleForm() {
      if (this.value3 === false) {
        this.addForm.isPush = '0'
      } else {
        this.addForm.isPush = '1'
      }
      this.$refs.addForm.validate(valid => {
        if (valid) {
          this.addForm.password = md5(this.addForm.passwordCheck)
          addPropertyUser(this.addForm).then(
            ({ data: { data, msg, code } }) => {
              if (code === 0) {
                this.doQuery()
                this.dialogFormVisible = false
              }
              this.$message({
                showClose: true,
                message: msg,
                type: code === 0 ? 'success' : 'error'
              })
            }
          )
        } else {
          return false
        }
      })
    },
    editPropertyUser({ rowId, userId, realName, phone, isPush }) {
      if (isPush === '0') {
        this.value2 = false
      } else {
        this.value2 = true
      }
      this.dialogEditFormVisible = true
      this.editExample = {
        rowId: rowId,
        realName: realName,
        phone: phone,
        isPush: isPush,
        userId: userId
      }
    },
    editExampleForm() {
      if (this.value2 === false) {
        this.editExample.isPush = '0'
      } else {
        this.editExample.isPush = '1'
      }
      this.$refs.editExample.validate(valid => {
        if (valid) {
          editPropertyUser(this.editExample).then(
            ({ data: { data, msg, code } }) => {
              if (code === 0) {
                this.doQuery()
                this.dialogEditFormVisible = false
              }
              this.$message({
                showClose: true,
                message: msg,
                type: code === 0 ? 'success' : 'error'
              })
            }
          )
        } else {
          return false
        }
      })
    }
  },
  components: {}
}
</script>

<style scoped></style>
